// pages/reportFormEfficiency/reportFormEfficiency.js
const app = getApp();
import CommonService from '../../utils/service/commonService.js';
const util = require('../../utils/util');
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

Component({
  /**
   * 组件的属性列表
   */
  properties: {},
  options: {   
    //虚拟的，这样可以不作用样式
    virtualHost: true    
  },

  /**
   * 组件的初始数据
   */
  data: {    
    //排名数据
    list:[],    
    //选择周
    showWeek:false,
    //周的选择列表
    weekList:[],
    //显示的周
    weekWord:'上周',
    //选中的周
    selectedWeek:{
      start:'',
      end:''
    },
    //周统计到几周前
    weekNum:7,
    //无数据
    showNothing:false
  },

  lifetimes: {
    // 在组件实例进入页面节点树时执行
    attached: function() {
      //计算本周和上几周的日期
      this.getWeekList();    
      //获取排名数据
      this.getData();
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {   
  //获取排名数据
  getData(){
    // Toast.loading({
    //   duration: 0,
    //   forbidClick: false,
    //   message: '效率排名数据加载中...'
    // });
    this.setData({ 
      list: [],  
      showNothing: false
    })   

    let week=this.data.selectedWeek;
    let param={
      departId:app.globalData.userInfo.depart,
      beginDate:week.start,
      endDate:week.end
    }

    CommonService.recordOfWeek(param).then(res=>{
      Toast.clear();
      console.log('效率排名数据');
      console.log(res.data.data);

      if(res.data.data && res.data.data.length>0){
        this.setData({
          list: res.data.data 
        })
      }else{
        this.setData({ 
          showNothing: true
        });
      }
    }).catch((error) => {
      Toast.clear();                
    })
  }, 

  //计算本周和上几周的日期
  getWeekList(){
    //当前日期
    let today=new Date();
    let day = today.getDay() || 7;
    //本周周一
    let monday=new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1 - day);
    //本周周日
    let sunday=new Date(today.getFullYear(), today.getMonth(), today.getDate() + 7 - day);    

    //本周
    let weekList=[{
      text:util.timeFormatDay(monday) +' 至 '+ util.timeFormatDay(today),                     
      start:util.timeFormatDay(monday),
      end:util.timeFormatDay(today),
      word:util.timeFormatDay(monday).slice(5) +' 至 '+ util.timeFormatDay(today).slice(5) 
    }];

    let n=this.data.weekNum;    
    for(let i=1;i<=n;i++){        
      let lastMonday=util.countPrevDay(monday,7);
      let lastSunday=util.countPrevDay(sunday,7);
      let obj={ 
        text:util.timeFormatDay(lastMonday) +' 至 '+ util.timeFormatDay(lastSunday),
        start:util.timeFormatDay(lastMonday),
        end:util.timeFormatDay(lastSunday),
        word:util.timeFormatDay(lastMonday).slice(5) +' 至 '+ util.timeFormatDay(lastSunday).slice(5)        
      }           
      weekList.push(obj);
    }
    
    this.setData({ 
      weekList: weekList,
      //周统计初始参数赋值
      selectedWeek:{
        start:weekList[1].start,
        end:weekList[1].end
      }     
    })       
  },

  //显示选择周的日期
  onShowWeekSelect(){
    this.setData({ 
			showWeek: true
    })  
  },

  //隐藏选择周的日期
  onCloseWeek(){
    this.setData({ 
			showWeek: false
    })  
  },

  //确认选择周的日期
  onConfirmWeek(e){
    let obj=e.detail.value;    
    this.setData({ 
      weekWord: obj.word,
      selectedWeek:obj
    })
    
    //隐藏选择周的日期
    this.onCloseWeek();

    //获取排名数据    
    this.getData();
  },

  //全部排名
  onShowAll(e){
    let detail=e.currentTarget.dataset.detail;
    let list=this.data.list;
    list.forEach(item => {
		  if (item.categoryName==detail){
        item.showAll=false;
        item.limit=item.con.length;
		  }
		});
		this.setData({ 
      list: list
    })  
  },

  //只看前十
  onShowTop(e){
    let detail=e.currentTarget.dataset.detail;
    let list=this.data.list;
    list.forEach(item => {
		  if (item.categoryName==detail){
        item.showAll=true;
        item.limit=10;
		  }
		});
		this.setData({ 
      list: list
    })  
  },
    
  }
})